package com.eastfair.home.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeServiceBorrowReturnService;
import com.eastfair.venuebooking.api.ServiceBorrowReturnFeign;
import com.eastfair.venueservice.dto.ServiceBorrowReturnDTO;
import com.eastfair.venueservice.vo.ServiceBorrowReturnVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 服务借还服务.
 *
 * @author dqq
 * @date 2022 /9/20
 */
@Service
@Slf4j
public class HomeServiceBorrowReturnServiceImpl implements HomeServiceBorrowReturnService {

    @Resource
    private ServiceBorrowReturnFeign serviceBorrowReturnFeign;

    @Override
    public Page<ServiceBorrowReturnVO> queryServiceBorrowReturn(PageParams<ServiceBorrowReturnDTO> pageParams) {
        log.info("queryServiceBorrowReturn = 分页查询借还记录VO列表, pageParams={}", pageParams);
        R<Page<ServiceBorrowReturnVO>> result = serviceBorrowReturnFeign.queryServiceBorrowReturn(pageParams);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public ServiceBorrowReturnVO getById(Long id) {
        log.info("getById - ID获取借还记录, id={}", id);
        R<ServiceBorrowReturnVO> result = serviceBorrowReturnFeign.getById(id);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean borrow(ServiceBorrowReturnDTO serviceBorrowReturnDTO) {
        log.info("borrow - 借用服务, serviceBorrowReturnDTO={}", serviceBorrowReturnDTO);
        R<Boolean> result = serviceBorrowReturnFeign.borrow(serviceBorrowReturnDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean returnService(ServiceBorrowReturnDTO serviceBorrowReturnDTO) {
        log.info("returnService - 归还服务, serviceBorrowReturnDTO={}", serviceBorrowReturnDTO);
        R<Boolean> result = serviceBorrowReturnFeign.returnService(serviceBorrowReturnDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }
}